package com.demo.cloud.service;

import com.demo.cloud.vo.UserVo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class DemoService {

    @Autowired
    private RestTemplate restTemplate;

    public String hi(String name) {
        return restTemplate.getForObject("http://service-1/demo/hello?name=" + name, String.class);
    }

    @HystrixCommand(fallbackMethod = "setUserError")
    public String setUser(UserVo vo) {
        vo.setAge(20);
//        restTemplate.getMessageConverters().add(newMappingJacksonHttpMessageConverter());
        return restTemplate.postForObject("http://service-1/demo/changeUser", vo, UserVo.class).toString();
    }

    /**
     * 方法参数列表必须跟调用一样
     * @param vo 参数
     * @return 错误提示信息
     */
    private String setUserError(UserVo vo) {
        return "set user error";
    }
}
